<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<!DOCTYPE html>
<html>
<head>
 <meta charset="UTF-8">
 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
 <meta name="viewport" content="width=device-width, initial-scale=1,user-scalable=0">
 <script src="${pageContext.request.contextPath }/js/jquery-1.9.1.min.js"></script>
 <title>获取微信授权登陆</title>
 
</head>
<body>
	<div style="margin: auto;width: 300px;"align="center">
	
		 <!-- 
		 	这里只修改了appid、
		 	redirect_uri（回调请求地址，这里的当前项目接口地址是http://192.168.137.21:8080/TestWXAuth/weixin/auth）
		 	以及scope三个参数，其余的参数保持默认。
		  -->
		<!--  
		
		
		<a href="https://open.weixin.qq.com/connect/oauth2/authorize
		?appid=wx8249cffa42281f99
		&redirect_uri=http://192.168.1.7:8080/TestWXAuth/weixin/auth
		&response_type=code
		&scope=snsapi_userinfo
		&state=STATE#wechat_redirect">
		微信授权登陆</a> 
		
		-->
		
		微信二维码扫码登陆
		<button onclick="smdl()">生成二维码</button>		 
		 <img id="qrCodeImg"/>
	</div>
	
</body>
<script type="text/javascript">
	 var interval;//定时器
	 var token="123456";//这里token是个唯一标识，用户服务端判断用户登陆状态,可自由随机生成
	 
	 
	 	function smdl(){
	 		$("#qrCodeImg").attr('src',"${ctx}/weixin/getCodeImage?token="+token);
		     interval = setInterval(function() {
		    	 checkCustAuthState();
		     }, 2000);
	 			
	 	}
	 
	 
		// 校验用户授权状态
	    function checkCustAuthState() {
	        $.ajax({
	        	url:"${ctx}/weixin/checkAuthState",
				type:"post",
				cache:false,
				data:{
					"token" : token
				},
				dataType:"json",
	            success : function(data) {
	                if (data.code == 200) {//成功
	                    clearInterval(interval);
						alert("授权登陆成功！跳转成功页面");
						
	                }else if(data.code == 500){
	                    // alert(data.code)
	                    clearInterval(interval);
	                    alert(data.msg);
	                   
	                }
	            }
	        });
	    }
	 
 </script>
</html>